package com.shidaimanong.demo.regex;

import java.util.regex.Pattern;

/**
 * 学编程，关注：十代码农
 * 抖音、快手、B站、西瓜视频、今日头条
 */
public class EmailRegex {
    public static void main(String[] args) {
        EmailRegex emailRegex = new EmailRegex();
        String content = "ss@ww.com";
        if (emailRegex.isValidEmail(content)) {
            System.out.println("合法邮箱");
        } else {
            System.out.println("不合格");
        }
    }

    /**
     * 判断email是否是合法邮箱
     */
    private boolean isValidEmail(String email) {
        String pattern = "[a-z]+@[a-z]+\\.[a-z]+";
        return Pattern.matches(pattern, email);
    }

    /**
     * 判断email是否是合法邮箱
     */
    private boolean isValidEmail2(String email) {
        if (email.isEmpty()) {
            return false;
        }
        //判断是否包含@
        if (!email.contains("@")) {
            return false;
        }
        //判断是否包含.
        if (!email.contains(".")) {
            return false;
        }

        //替换掉字符串中@和.，然后检查剩下的部分是否都是字母
        email = email.replace("@", "")
                .replace(".", "");

        char[] chars = email.toCharArray();
        //判断是否都是字母
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] < 'a' || chars[i] > 'z') {
                return false;
            }
        }
        return true;
    }
}
